< previous page page_195 next page >

Page 195
numvar represents a numeric variable that stores the output of the function. inptstr can be a literal string, a string variable, or a string function. The Val function first strips out any spaces from the string and then starts reading the numbers in the string. If the first character in the string is not a number (or minus sign), Val returns 0. Otherwise, Val reads the string until it encounters a nonnumeric character. At this point, it stops reading and converts the digits it has read into a number.
note.gif When building a string for display with concatenation, you can implicitly convert a number to a string, as follows:
Dim sOutput As String
Dim nWeight As Integer
nWeight = 145
sOutput = "The answer is " & nWeight & " pounds."
 square.gif

Formatting Results
When information, such as a number, is stored in a variable, it is stored in a format that you need in order to manipulate it with program code. However, often you need to change the format of information before displaying it or printing it on a report. Suppose, for example, you have several numbers stored in variables that represent dollar amounts. If the numbers are the result of a division, they may contain extra decimal places, and if you print them as is, they will probably not be aligned the way you want them.
In this section, you'll look at a few functions designed to help you format numbers and other information. In particular, you'll look at the Format function, which has been around a long time, and some more specific formatting functions introduced in Visual Basic 6.
Specific Formatting Functions
Visual Basic provides a number of formatting functions to format specific types of information. These functions are listed in Table 9.4.
Table 9.4 New Formatting Functions Introduced in Visual Basic 6
FunctionUsed ForInputOne Possible Output
FormatCurrencyMoney8675.309$8,675.31
FormatNumberNumbers-5000(5,000.00)
FormatPercentPercents0.123412.34%
FormatDateTimeDate/Time"12-31 13:34"12/31/98 1:34:00 PM
RoundNumbers123.6124

All the functions listed in Table 9.4 return string values. Simply pass the function an expression (which may or may not be a string value) and some optional parameters that control the output.

 
< previous page page_195 next page >